import {
	ref
} from 'vue'
import initApi from '@/apis/init.js';
export default function useSendCode(formData) {
	const isSend = ref(false);

	const isNum = ref(0);
	const isTimer = ref();
	const sendCode = async (flag) => {
		const isOkMobile = /^1[3-9][0-9]\d{8}$/.test(formData.mobile);
		if (!isOkMobile) return uni.utils.toast('请输入正确的手机号');
		if (!formData.allChecked) return uni.utils.toast('请勾选《活动规则》《隐私协议》');
		if (isSend.value) return;
		try {
			const arr = {
				mobile: formData.mobile,
				channelCode: formData.channelCode,
				productCode: formData.productCode
			};

			const res = await initApi.initInventoryPackageOrder(arr);
			// 后期 是否要判断 归宿地
			if (res.code != 20000) {
				return uni.utils.toast(res.message);
			}

			formData.orderNo = res.data.orderNo;
			isNum.value = 60;
			isSend.value = true;
			isTimer.value = setInterval(() => {
				if (isNum.value <= 0) {
					clearInterval(isTimer.value);
					isNum.value = 0;
					isSend.value = false;
				}
				isNum.value--;
			}, 1000);
			return uni.utils.toast('验证码发送成功');
		} catch (e) {
			console.log(e);
		}
	};
	return {
		sendCode,
		isSend,
		isTimer,
		isNum
	}
}